/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-09-29     FuLei       the first version
 */
#ifndef MCU_HW_MYSPI_C_
#define MCU_HW_MYSPI_C_

#include <rtdevice.h>
#include <rtthread.h>
#include <board.h>

void SPI1_Init(void)
{
    struct rt_spi_device *spi_device;
    struct rt_spi_configuration spi_cfg;
    rt_err_t err_status;

    // --------1)  将SPI设备挂载到SPI总线上-------------------
    spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));


    // "spi1" 表示硬件SPI1，"spi10"表示SPI1的SS0，片选0，
    // 若SPI1有4个片选，则"spi10"  "spi11" "spi12"  "spi13"
    rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);   // PA4为片选

    spi_device = (struct rt_spi_device *)rt_device_find("spi10");

    //  --------2) SPI模式配置   -------- -------- --------
    spi_cfg.data_width = 8;
    spi_cfg.mode       = RT_SPI_MASTER | RT_SPI_MODE_3 | RT_SPI_MSB;
    spi_cfg.max_hz     = 2 * 100 *1000; /* 2M,SPI max 42MHz,ssd1351 4-wire spi */

    err_status = rt_spi_configure(spi_device, &spi_cfg);
    if( err_status == RT_EOK )
    {
        rt_kprintf("spi1 configure OK\n");
    }
    else {
        rt_kprintf("spi1 configure fail\n");
    }
}




#endif /* MCU_HW_MYSPI_C_ */
